package com.gwt.client.login.client;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import com.jgoodies.forms.factories.DefaultComponentFactory;
import javax.swing.JButton;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import com.jgoodies.forms.factories.FormFactory;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.io.IOException;
import java.net.UnknownHostException;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


public class CRUDUserUI {

	private JFrame frmUserInformation;
	private JTextField txtName;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JTextField txtSsn;
	private JTextField txtPhone;
	private JTextField txtEmail;
	private String name;
	private String pass;
	private String type = "Patient";
	private JButton btnBack;
	private JButton btnResetPassword;
	private JButton btnEdit;
	
	private Transmitter tm;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CRUDUserUI window = new CRUDUserUI();
					window.frmUserInformation.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public CRUDUserUI() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmUserInformation = new JFrame();
		frmUserInformation.setTitle("User Information");
		frmUserInformation.setBounds(100, 100, 450, 300);
		frmUserInformation.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		txtName = new JTextField();
		txtName.setEditable(false);
		txtName.setText("Name");
		txtName.setColumns(10);
		
		txtSsn = new JTextField();
		txtSsn.setEditable(false);
		txtSsn.setText("SSN");
		txtSsn.setColumns(10);
		
		txtPhone = new JTextField();
		txtPhone.setEditable(false);
		txtPhone.setText("Phone");
		txtPhone.setColumns(10);
		
		txtEmail = new JTextField();
		txtEmail.setEditable(false);
		txtEmail.setText("Email");
		txtEmail.setColumns(10);
		
		JButton btnCreateUser = new JButton("Delete User");
		btnCreateUser.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//create user here
				//bring back admin screen
//				try {
//					tm.open();
//				} catch (UnknownHostException e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				} catch (IOException e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				}
//				try {
//					tm.send("deleteuser,");
//				} catch (IOException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
//				try {
//					tm.CloseSocket();
//				} catch (IOException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//				}
				AdminUI am = new AdminUI();
				am.setVisible(true);
				frmUserInformation.setVisible(false);
				frmUserInformation.dispose();
			}
		});
		
		btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//create user here
				//bring back admin screen
				AdminUI am = new AdminUI();
				am.setVisible(true);
				frmUserInformation.setVisible(false);
				frmUserInformation.dispose();
			}
		});
		
		final JButton btnClickToUpdate = new JButton("Click to Update");
		btnClickToUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtName.setEditable(false);
				txtSsn.setEditable(false);
				txtPhone.setEditable(false);
				txtEmail.setEditable(false);
				btnClickToUpdate.setEnabled(false);
//				try {
//					tm.open();
//				} catch (UnknownHostException e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				} catch (IOException e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				}
//				try {
//					tm.send("updateuser," + txtName.getText() + "," + txtSsn.getText() + "," + txtPhone.getText() + "," + txtEmail.getText());
//				} catch (IOException e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				}
//				try {
//					tm.CloseSocket();
//				} catch (IOException e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				}
			}
		});
		btnClickToUpdate.setEnabled(false);
		
		btnResetPassword = new JButton("Reset Password");
		btnResetPassword.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
//				try {
//					tm.open();
//				} catch (UnknownHostException e3) {
//					// TODO Auto-generated catch block
//					e3.printStackTrace();
//				} catch (IOException e3) {
//					// TODO Auto-generated catch block
//					e3.printStackTrace();
//				}
//				try {
//					tm.send("resetpassword,");
//				} catch (IOException e2) {
//					// TODO Auto-generated catch block
//					e2.printStackTrace();
//				}
//				try {
//					tm.CloseSocket();
//				} catch (IOException e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				}
			}
		});
		
		btnEdit = new JButton("Edit");
		btnEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				txtName.setEditable(true);
				txtSsn.setEditable(true);
				txtPhone.setEditable(true);
				txtEmail.setEditable(true);
				btnClickToUpdate.setEnabled(true);
			}
		});
		GroupLayout groupLayout = new GroupLayout(frmUserInformation.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(61)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
									.addComponent(txtPhone, Alignment.TRAILING)
									.addComponent(txtSsn, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
									.addComponent(txtEmail))
								.addComponent(txtName, GroupLayout.PREFERRED_SIZE, 137, GroupLayout.PREFERRED_SIZE))
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addGroup(groupLayout.createSequentialGroup()
									.addGap(59)
									.addComponent(btnResetPassword))
								.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
									.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
									.addComponent(btnEdit)
									.addGap(28))))
						.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
							.addComponent(btnClickToUpdate)
							.addGap(38)
							.addComponent(btnCreateUser)
							.addGap(18)
							.addComponent(btnBack)))
					.addContainerGap(68, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap(48, Short.MAX_VALUE)
					.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(txtName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(txtSsn, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(txtPhone, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(btnResetPassword)
							.addGap(32)
							.addComponent(btnEdit)
							.addGap(8)))
					.addGap(18)
					.addComponent(txtEmail, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addGap(35)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnCreateUser)
						.addComponent(btnBack)
						.addComponent(btnClickToUpdate))
					.addGap(22))
		);
		frmUserInformation.getContentPane().setLayout(groupLayout);
	}
	/**
	 * 
	 * @param b
	 */
	public void setVisible(boolean b) {
		this.frmUserInformation.setVisible(b);		
	}
}
